<?php
/**
 * Created by PhpStorm.
 * User: Hk
 * Date: 2021/1/28
 * Time: 11:28
 */

namespace App\Http\Controllers\Admin;


use App\Http\Controllers\Controller;
use App\Models\Property;
use App\Models\PropertyCheck;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\AdminEstate;
use Illuminate\Support\Facades\Hash;
use Mockery\Exception;

class PropertyController extends Controller
{
    public $model;
    public function __construct()
    {
        $this->model = new Property();
    }

    /**
     * Notes: 物业列表
     * User: HK
     * Date: 2021/1/28
     * Time: 11:28
     * email: 1919194843@qq.com
     */
    public function index(Request $request)
    {
        $param = $request->query();

        if (isset($param['name']) && !empty($param['name'])) $where[] = [ 'name' , 'like' , "%{$param['name']}%"];
        if (isset($param['mobile']) && !empty($param['mobile'])) $where[] = [ 'principal_mobile' , '=' , $param['mobile']];
        if (isset($param['status']) && !empty($param['status'])) $where[] = [ 'status' , '=' , $param['status']];
        if ($param['check'] == 1)  $where[] =  [ 'check' ,'=', $param['check'] ];
        else  $where[] = [ 'check' ,'!=', 1 ];
        $where[] = [ 'status','>', 0 ];

        return $this->success( $this->model->getPaginate( $where ) );
    }

    /**
     * Notes: 修改物业状态
     * User: HK
     * Date: 2021/1/28
     * Time: 13:58
     * email: 1919194843@qq.com
     */
    public function edit_status (Request $request)
    {
        $id = $request->post('id');
        $status = $this->model->getValue(['id' => $id],'status') == 1 ? 2 : 1;
        return $this->model->updateByWhere(['id' => $id],['status' => $status]) ? $this->success('修改成功!') : $this->error('修改失败!');
    }

    /**
     * Notes: 删除物业
     * User: HK
     * Date: 2021/1/28
     * Time: 14:11
     * email: 1919194843@qq.com
     * @param $ids
     * @return array
     */
    public function destroy ($ids)
    {
        return $this->model->deleteAll( $ids ) ? $this->success('删除成功!') : $this->error('删除失败!');
    }

    /**
     * Notes: 查询审核资料信息
     * User: HK
     * Date: 2021/1/28
     * Time: 15:18
     * email: 1919194843@qq.com
     */
    public function show( $id )
    {
        return $this->success( (new PropertyCheck())->getInfo( [ 'property_id' =>  $id ] ) );
    }

    /**
     * Notes: 编辑查询表单
     * User: HK
     * Date: 2021/1/28
     * Time: 15:18
     * email: 1919194843@qq.com
     */
    public function edit( $id )
    {
        return $this->success( $this->model->getInfoById( $id ) );
    }

    /**
     * Notes: 保存编辑的数据
     * User: HK
     * Date: 2021/1/28
     * Time: 17:25
     * email: 1919194843@qq.com
     */
    public function update(Request $request)
    {
        $data = $request->post();

        if ($data['password'] == null) unset($data['password']);
        else $data['password'] = md5($data['password']);

        unset($data['create_time']);

        return $this->model->updateById( $data['id'] , $data ) ? $this->success('修改成功!') : $this->error('修改失败!');
    }

    /**
     * Notes: 修改审核状态
     * User: HK
     * Date: 2021/1/28
     * Time: 15:54
     * email: 1919194843@qq.com
     */
    public function edit_check (Request $request,AdminEstate $admin_model)
    {
        $data = $request->post();

        if (isset($data['check']) && !empty($data['check'])){
            if ( empty( $data['reason_rejection'] ) ) return $this->error('请输入驳回原因!');
            $arr = [ 'check' => 2 ];
            $check_arr = [ 'check' => 2 , 'reason_rejection' => $data['reason_rejection'] ];
        }else{
            $arr = [ 'check' => 1];
            $check_arr = [ 'check' => 1 ];
        }
        try {
            $this->model->updateById( $data['id'] , $arr );
            $user=$this->model->getInfoById( $data['id'] );
            $admin_model = $admin_model->create([
                'username'  => $user['account'],
                'password'  => $user['password'],
                'nickname'  => '管理员',
                'avatar'    => '',
                'estate_id' => $user['id'],
                'pid'       => 0,
            ]);
            ( new PropertyCheck() )->updateById( $data['property_id'] , $check_arr );
            return $this->success('修改成功!');
        } catch (Exception $exception){
            return $this->error('修改失败!');
        }

    }

    public function all_property()
    {
        return $this->success( $this->model->where(['status'=>1])->select('id','name')->get());
    }
}
